// import { IForm } from '@/components/myForm/types/index';
import { ref } from 'vue';

interface IModelData {
  [key: string]: any;
}

export const usePageModel = function (modelConfig) {
  const modelData = ref<IModelData>(
    modelConfig.value.formItems.reduce((prev, curr) => {
      prev[curr.key] = '';
      return prev;
    }, {})
  );

  const setModelData = function (value: IModelData) {
    Object.keys(modelData.value).map((key) => {
      if (key in value) {
        modelData.value[key] = value[key];
      } else {
        modelData.value[key] = '';
      }
    });
  };

  const clearModelData = function () {
    setModelData({});
  };

  const searchFormItem = function (key: string) {
    return modelConfig.value.formItems.find((o) => o.key === key);
  };

  return {
    modelData,
    setModelData,
    clearModelData,
    searchFormItem,
  };
};
